﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using GeneralTools;

namespace MtgPrinter
{
    static class Program
    {
        public static MtgPrinterConfiguration AppConfig
        {
            get;
            set;
        }

        public static string getWorkpath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MtgPrinter\\";
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                AppConfig = MtgPrinterConfiguration.Deserialize<MtgPrinterConfiguration>(getWorkpath() + "settings.xml");
            } 
             catch(FileNotFoundException) {}
             catch(Exception e)
            {
                using(ExceptionForm exf=new ExceptionForm(e))
                {
                    exf.ShowDialog();
                }
            } finally
            {
                if (AppConfig == null) AppConfig = new MtgPrinterConfiguration();
            }

            Form1 frm1 = new Form1();
            frm1.FormClosed += new FormClosedEventHandler(frm1_FormClosed);
            Application.Run(frm1);
        }

        static void frm1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                MtgPrinterConfiguration.Serialize(AppConfig, getWorkpath() + "settings.xml");
            } catch(Exception ex)
            {
                using (ExceptionForm exf = new ExceptionForm(ex))
                {
                    exf.ShowDialog();
                }
            }
        }
    }
}
